var RawInputLogger = InputLogger.extend({
	buffer: null,
	bufferc: 0,
	uploading: false,
	buffer_limit: 30,
	ref_time: null,
	constructor: function()
	{
	},
	LoggerName: function()
	{
		return 'RawInputLogger';
	},
	onInit: function()
	{
		this.buffer = new Array();
		this.bufferc = 0;
		this.uploading = false;
		this.ref_time = 0;
		this.push_buffer('N',0,0);
		this.ref_time = (new Date()).getTime();
	},
	onShutdown: function()
	{
	},
	push_buffer: function(type,key,mod)
	{
		var o;

		o = {};
		o.time = (new Date()).getTime() - this.ref_time;
		o.time = o.time.toString();
		o.type = type;
		o.key = key;
		o.mod = mod;
		this.buffer[this.bufferc++] = o;
		return this.bufferc;
	},
	flush_buffer: function()
	{
		var url,i,o;
		var x = this;

		if(this.uploading) return false;
		this.uploading = true;

		url = 'http://127.0.0.1/backend/rawlog.php?count=' + this.bufferc;
		for(i=0;i<this.bufferc;i++) {
			o = this.buffer[i];
			url += '&time' + i + '=' + o.time;
			url += '&type' + i + '=' + o.type;
			url += '&key' + i + '=' + o.key;
			url += '&mod' + i + '=' + o.mod;
		}
		DownloadManager.NewDownloadName('RawInputLogger',url,function(userdata,serverdata) {
			userdata.Callback(serverdata);
		},this);
		return true;
	},
	Callback: function(count)
	{
		if(count < 0) {
			Debug.trace(0,"RawInputLogger: backend have errors.");
		}
		else {
			this.bufferc -= count;
			this.buffer = this.buffer.slice(count);
			Debug.trace(0,"RawInputLogger: uploaded %d record(s), remaining %d in the buffer.",count,this.bufferc);
			this.uploading = false;
		}
	},
	keypress_log: function(key,modifier)
	{
		if(this.push_buffer('P',key,modifier) > this.buffer_limit) this.flush_buffer();
	},
	keyup_log: function(key,modifier)
	{
		if(this.push_buffer('U',key,modifier) > this.buffer_limit) this.flush_buffer();
	},
	keydown_log: function(key,modifier)
	{
		if(this.push_buffer('D',key,modifier) > this.buffer_limit) this.flush_buffer();
	}
});
